package com.zhiche.lisa.bms.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.pojo.vo.util.ShipOrderItemVO;
import com.zhiche.lisa.bms.pojo.vo.util.ShipTaskVO;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class TmsUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(TmsUtil.class);

    @Value("${lisa.tms.url}")
    private String url;
    @Value("${lisa.tms.getShipTaskByCodeUrl}")
    private String getShipTaskByCodeUrl;
    @Value("${lisa.tms.getShipOrderItemByIdUrl}")
    private String getShipOrderItemByIdUrl;
    @Value("${lisa.socketTimeout}")
    private Integer socketTimeout;

    //@Value("${lisa.tms.getOrderItemByItemSourceKeyURL}")
    //private String getShipOrderItemByItemSourceKeyURL;

    public RestfulResponse<ShipTaskVO> getShipTaskByCode(String code, String token) throws BaseException {
        if (!StringUtils.hasText(code)) {
            throw new BaseException("运输任务编号，不能为空！");
        }
        if (!StringUtils.hasText(token)) {
            throw new BaseException("token鉴权，不能为空！");
        }
        // 发送请求给tms
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", token));
        String tmsUrl = url + getShipTaskByCodeUrl + "/" + code;
        String strReturn = HttpClientUtil.get(tmsUrl, headNamePairs, Lists.newArrayList(), socketTimeout);

        if (!StringUtils.isEmpty(strReturn)) {
            RestfulResponse<ShipTaskVO> shipTaskVO = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<ShipTaskVO>>() {
            });
            return shipTaskVO;
        } else {
            return null;
        }
    }

    public RestfulResponse<ShipOrderItemVO> getShipOrderItemById(Long orderItemId, String token) throws BaseException {
        if (ObjectUtils.isEmpty(orderItemId)) {
            throw new BaseException("运单货物明细ID，不能为空！");
        }
        // 发送请求给tms
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", token));
        String tmsUrl = url + getShipOrderItemByIdUrl + "/" + orderItemId;
        String strReturn = HttpClientUtil.get(tmsUrl, headNamePairs, Lists.newArrayList(), socketTimeout);
        if (!StringUtils.isEmpty(strReturn)) {
            RestfulResponse<ShipOrderItemVO> shipOrderItemVO = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<ShipOrderItemVO>>() {
            });
            return shipOrderItemVO;
        } else {
            return null;
        }
    }
    //public OrderItemDTO getShipOrderItemByItemSourceKey (String itemSourceKey, String token)throws BaseException {
    //    List<NameValuePair> headNamePairs = Lists.newArrayList();
    //    headNamePairs.add(new BasicNameValuePair("Authorization", token));
    //    String omsUrl = url + getShipOrderItemByItemSourceKeyURL + "/" + itemSourceKey;
    //    String strReturn = HttpClientUtil.get(omsUrl, headNamePairs, Lists.newArrayList(), socketTimeout);
    //    if (org.apache.commons.lang3.StringUtils.isNotEmpty(strReturn)) {
    //        RestfulResponse<OrderItemDTO> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<OrderItemDTO>>() {
    //        });
    //        return restfulResponse.getData();
    //    } else {
    //        return null;
    //    }
    //}
}
